package com.shanxi.cz.module.biz.controller.admin.ammeterbasicinfo;

import com.mzt.logapi.starter.annotation.LogRecord;
import com.shanxi.cz.module.infra.controller.admin.config.vo.ConfigSaveReqVO;
import com.shanxi.cz.module.infra.service.config.ConfigService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.shanxi.cz.framework.common.pojo.PageParam;
import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.pojo.CommonResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import static com.shanxi.cz.framework.common.pojo.CommonResult.success;

import com.shanxi.cz.framework.excel.core.util.ExcelUtils;

import com.shanxi.cz.framework.apilog.core.annotation.ApiAccessLog;
import static com.shanxi.cz.framework.apilog.core.enums.OperateTypeEnum.*;

import com.shanxi.cz.module.biz.controller.admin.ammeterbasicinfo.vo.*;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterbasicinfo.AmmeterBasicInfoDO;
import com.shanxi.cz.module.biz.service.ammeterbasicinfo.AmmeterBasicInfoService;
import static com.shanxi.cz.module.biz.enums.LogRecordConstants.*;

@Tag(name = "管理后台 - 电表基础信息")
@RestController
@RequestMapping("/biz/ammeter-basic-info")
@Validated
public class AmmeterBasicInfoController {

    @Resource
    private AmmeterBasicInfoService ammeterBasicInfoService;

    @Resource
    private ConfigService configService;

    @PostMapping("/create")
    @Operation(summary = "创建电表基础信息")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:create')")
    public CommonResult<Integer> createAmmeterBasicInfo(@Valid @RequestBody AmmeterBasicInfoSaveReqVO createReqVO) {
        return success(ammeterBasicInfoService.createAmmeterBasicInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电表基础信息")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:update')")
    public CommonResult<Boolean> updateAmmeterBasicInfo(@Valid @RequestBody AmmeterBasicInfoSaveReqVO updateReqVO) {
        ammeterBasicInfoService.updateAmmeterBasicInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-threshold")
    @Operation(summary = "设置阈值")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:update-threshold')")
    @LogRecord(type = SYSTEM_USER_TYPE, subType = SET_DAY_REPORT_SUB_TYPE, bizNo = "{{#configSaveReqVO.value}}",
            success = SET_DAY_REPORT_SUCCESS)
    public CommonResult<Boolean> updateThreshold(@Valid @RequestBody ConfigSaveReqVO configSaveReqVO) {
        configService.updateConfig(configSaveReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电表基础信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:delete')")
    public CommonResult<Boolean> deleteAmmeterBasicInfo(@RequestParam("id") Integer id) {
        ammeterBasicInfoService.deleteAmmeterBasicInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除电表基础信息")
                @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:delete')")
    public CommonResult<Boolean> deleteAmmeterBasicInfoList(@RequestParam("ids") List<Integer> ids) {
        ammeterBasicInfoService.deleteAmmeterBasicInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电表基础信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:query')")
    public CommonResult<AmmeterBasicInfoRespVO> getAmmeterBasicInfo(@RequestParam("id") Integer id) {
        AmmeterBasicInfoDO ammeterBasicInfo = ammeterBasicInfoService.getAmmeterBasicInfo(id);
        return success(BeanUtils.toBean(ammeterBasicInfo, AmmeterBasicInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电表基础信息分页")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:query')")
    public CommonResult<PageResult<AmmeterBasicInfoRespVO>> getAmmeterBasicInfoPage(@Valid AmmeterBasicInfoPageReqVO pageReqVO) {
        PageResult<AmmeterBasicInfoDO> pageResult = ammeterBasicInfoService.getAmmeterBasicInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AmmeterBasicInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电表基础信息 Excel")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-basic-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAmmeterBasicInfoExcel(@Valid AmmeterBasicInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AmmeterBasicInfoDO> list = ammeterBasicInfoService.getAmmeterBasicInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电表基础信息.xls", "数据", AmmeterBasicInfoRespVO.class,
                        BeanUtils.toBean(list, AmmeterBasicInfoRespVO.class));
    }

}